package com.zhangxiaofanfan.hotcode.fourth.third;


import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author zhangxiaofanfan
 * @date 2024-06-26 20:33:33
 */
public class HotCode059 {
    public static void main(String[] args) {
        HotCode059 hotCode059 = new HotCode059();
        System.out.println(hotCode059.generateParenthesis(3));
    }

    private List<String> result = new ArrayList<>();
    public List<String> generateParenthesis(int n) {
        bfs(n, 0, new StringBuffer());
        return result;
    }

    public void bfs(int n, int prefixCount, StringBuffer sb) {
        if (prefixCount > n || prefixCount * 2 < sb.length() || sb.length() > n * 2) {
            return;
        }
        if (sb.length() == n * 2) {
            result.add(new String(sb));
            return;
        }
        bfs(n, prefixCount + 1, sb.append("("));
        sb.deleteCharAt(sb.length() - 1);
        bfs(n, prefixCount, sb.append(")"));
        sb.deleteCharAt(sb.length() - 1);
    }

}
